// Project: RacingGame, File: GameSettings.cs
// Namespace: RacingGame.Properties, Class: GameSettings
// Path: C:\code\RacingGame\Properties, Author: Abi
// Code lines: 455, Size of file: 10,03 KB
// Creation date: 23.10.2006 17:21
// Last modified: 23.10.2006 22:48
// Generated with Commenter by abi.exDream.com

#region Using directives
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using Helper;
using System.Threading;
#endregion

namespace SpaceWolf.Properties
{
    /// <summary>
    /// Game settings, stored in a custom xml file. The reason for this is
    /// we want to be able to store our game data on the Xbox360 too.
    /// On the PC we could just use a Settings/config file and have all the
    /// code autogenerated for us, but this way it works both on PC and Xbox.
    /// Note: The default instance for the game settings is in this class,
    /// this way we get the same behaviour as for normal Settings files!
    /// </summary>
    [Serializable]
    public class GameSettings
    {
        #region Default

        /// <summary>
        /// Filename for our game settings file.
        /// </summary>
        const string SettingsFilename = "SpaceWolfSettings.xml";

        /// <summary>
        /// Default instance for our game settings.
        /// </summary>
#if !XBOX360
        [NonSerialized]
#endif
        private static GameSettings defaultInstance = null;

        /// <summary>
        /// Need saving the game settings file? Only set to true if
        /// we really changed some game setting here.
        /// </summary>
#if !XBOX360
        [NonSerialized]
#endif
        private static bool needSave = false;

        /// <summary>
        /// Default
        /// </summary>
        /// <returns>Game settings</returns>
        public static GameSettings Default
        {
            get
            {
                return defaultInstance;
            } // get
        } // Default
        #endregion

        #region Constructor
        /// <summary>
        /// Create game settings, don't allow public constructor!
        /// </summary>
        private GameSettings()
        {
        } // GameSettings()

        /// <summary>
        /// Create game settings. This constructor helps us to only load the
        /// GameSettings once, not again if GameSettings is recreated by
        /// the Deserialization process.
        /// </summary>
        /// <param name="loadSettings">Load settings</param>
        static GameSettings()
        {
            defaultInstance = new GameSettings();
            Load();
        } // GameSettings(loadSettings)
        #endregion

        #region Load
        /// <summary>
        /// Load
        /// </summary>
        public static void Load()
        {
            needSave = false;

            FileStream file = Helper.FileHelper.LoadGameContentFile(
                SettingsFilename);

            if (file == null)
            {
                // Create new file after quitting
                needSave = true;
                return;
            } // if

            // If the file is empty, just create a new file with the default
            // settings.
            if (file.Length == 0)
            {
                // Close the file
                file.Close();

                // But first check if there is maybe a file in the game directory
                // to load the default game settings from.
                file = Helper.FileHelper.LoadGameContentFile(SettingsFilename);
                if (file != null)
                {
                    // Load everything into this class
                    GameSettings loadedGameSettings =
                        (GameSettings)new XmlSerializer(typeof(GameSettings)).
                        Deserialize(file);
                    if (loadedGameSettings != null)
                        defaultInstance = loadedGameSettings;

                    // Close the file
                    file.Close();
                } // if (file)

                // Save user settings file
                needSave = true;
                Save();
            } // if (file.Length)
            else
            {
                // Else load everything into this class with help of the
                // XmlSerializer.
                GameSettings loadedGameSettings =
                    (GameSettings)new XmlSerializer(typeof(GameSettings)).
                    Deserialize(file);
                if (loadedGameSettings != null)
                    defaultInstance = loadedGameSettings;

                // Close the file
                file.Close();
            } // else
        } // Load()
        #endregion

        #region Save
        /// <summary>
        /// Save
        /// </summary>
        public static void Save()
        {
            // No need to save if everything is up to date.
            if (needSave == false)
                return;

            needSave = false;

            FileStream file = Helper.FileHelper.SaveGameContentFile(
                SettingsFilename);

            // Save everything in this class with help of the XmlSerializer.
            new XmlSerializer(typeof(GameSettings)).
                Serialize(file, defaultInstance);

            // Close the file
            file.Close();
        } // Save()
        #endregion

        #region Setting variables with properties
        /// <summary>
        /// Highscores
        /// </summary>
        string highscores = "";
        /// <summary>
        /// Highscores
        /// </summary>
        /// <returns>String</returns>
        public string Highscores
        {
            get
            {
                return highscores;
            } // get
            set
            {
                if (highscores != value)
                    needSave = true;
                highscores = value;
            } // set
        } // Highscores

        /// <summary>
        /// Player name
        /// </summary>
        string playerName = "";
        /// <summary>
        /// Player name
        /// </summary>
        /// <returns>String</returns>
        public string PlayerName
        {
            get
            {
                return playerName;
            } // get
            set
            {
                if (playerName != value)
                    needSave = true;
                playerName = value;
            } // set
        } // PlayerName

        /// <summary>
        /// Resolution width
        /// </summary>
        int resolutionWidth = 0;
        /// <summary>
        /// Resolution width
        /// </summary>
        /// <returns>Int</returns>
        public int ResolutionWidth
        {
            get
            {
                return resolutionWidth;
            } // get
            set
            {
                if (resolutionWidth != value)
                    needSave = true;
                resolutionWidth = value;
            } // set
        } // ResolutionWidth

        /// <summary>
        /// Resolution height
        /// </summary>
        int resolutionHeight = 0;
        /// <summary>
        /// Resolution height
        /// </summary>
        /// <returns>Int</returns>
        public int ResolutionHeight
        {
            get
            {
                return resolutionHeight;
            } // get
            set
            {
                if (resolutionHeight != value)
                    needSave = true;
                resolutionHeight = value;
            } // set
        } // ResolutionHeight

        /// <summary>
        /// Fullscreen
        /// </summary>
        bool fullscreen = false;
        /// <summary>
        /// Fullscreen
        /// </summary>
        /// <returns>Bool</returns>
        public bool Fullscreen
        {
            get
            {
                return fullscreen;
            } // get
            set
            {
                if (fullscreen != value)
                    needSave = true;
                fullscreen = value;
            } // set
        } // Fullscreen

        /// <summary>
        /// Performance settings
        /// </summary>
        int performanceSettings = -1;
        /// <summary>
        /// Performance settings
        /// </summary>
        /// <returns>Int</returns>
        public int PerformanceSettings
        {
            get
            {
                return performanceSettings;
            } // get
            set
            {
                if (performanceSettings != value)
                    needSave = true;
                performanceSettings = value;
            } // set
        } // PerformanceSettings

        bool postScreenEffects = true;
        /// <summary>
        /// Post screen effects
        /// </summary>
        /// <returns>Bool</returns>
        public bool PostScreenEffects
        {
            get
            {
                return postScreenEffects;
            } // get
            set
            {
                if (postScreenEffects != value)
                    needSave = true;
                postScreenEffects = value;
            } // set
        } // PostScreenEffects

        bool shadowMapping = true;
        /// <summary>
        /// ShadowMapping
        /// </summary>
        /// <returns>Bool</returns>
        public bool ShadowMapping
        {
            get
            {
                return shadowMapping;
            } // get
            set
            {
                if (shadowMapping != value)
                    needSave = true;
                shadowMapping = value;
            } // set
        } // ShadowMapping

        bool highDetail = true;
        /// <summary>
        /// HighDetail
        /// </summary>
        /// <returns>Bool</returns>
        public bool HighDetail
        {
            get
            {
                return highDetail;
            } // get
            set
            {
                if (highDetail != value)
                    needSave = true;
                highDetail = value;
            } // set
        } // HighDetail

        /// <summary>
        /// Sound volume
        /// </summary>
        float soundVolume = 0.8f;
        /// <summary>
        /// Sound volume
        /// </summary>
        /// <returns>Float</returns>
        public float SoundVolume
        {
            get
            {
                return soundVolume;
            } // get
            set
            {
                if (soundVolume != value)
                    needSave = true;
                soundVolume = value;
            } // set
        } // SoundVolume

        /// <summary>
        /// Music volume
        /// </summary>
        float musicVolume = 0.6f;
        /// <summary>
        /// Music volume
        /// </summary>
        /// <returns>Float</returns>
        public float MusicVolume
        {
            get
            {
                return musicVolume;
            } // get
            set
            {
                if (musicVolume != value)
                    needSave = true;
                musicVolume = value;
            } // set
        } // MusicVolume

        /// <summary>
        /// Controller sensibility
        /// </summary>
        float controllerSensibility = 1;
        /// <summary>
        /// Controller sensibility
        /// </summary>
        /// <returns>Float</returns>
        public float ControllerSensibility
        {
            get
            {
                return controllerSensibility;
            } // get
            set
            {
                if (controllerSensibility != value)
                    needSave = true;
                controllerSensibility = value;
            } // set
        } // ControllerSensibility
        #endregion

        #region Unit Testing
#if DEBUG
        /// <summary>
        /// Test save game settings
        /// </summary>
        public static void TestSaveGameSettings()
        {
            // Just save some data for the highscores ^^
            GameSettings.Default.Highscores = "TestSaveGameSettings";
            // And save, on the Xbox360 we should get a dialog asking us
            // where we want to save our savegame file.
            GameSettings.Save();
        } // TestSaveGameSettings()
#endif
        #endregion
    } // class GameSettings
} // namespace RacingGame.Properties
